package com.hqyj.controller;

import com.hqyj.pojo.ManagerInfo;
import com.hqyj.pojo.Resultinfo;
import com.hqyj.pojo.UserInfo;
import com.hqyj.pojo.UserSerInfo;
import com.hqyj.service.DayInfoService;
import com.hqyj.service.ManagerInfoService;
import com.hqyj.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserInfoController {
    @Autowired
    UserInfoService userInfoService;
    //创建managerinfoservice的实现类对象

    @Autowired
    DayInfoService dayInfoService;

    //获取图片上传的配置路径
    @Value("${file.address}")
    String  fileAddress;

    //用户访问的图片路径
    @Value("${file.staticPath}")
    String  upload;


    //访问当前用户的数据的页面user-info
    @RequestMapping("/infoPage")
    public String info(UserInfo user, ModelMap m, HttpSession session){
        //把当前用户名存入pojpo中并备份中也存入
        UserInfo userInfo= (UserInfo) session.getAttribute("user");
        System.out.print(userInfo.getUserName());
        user.setUserName(userInfo.getUserName());
        //根据用户名查询
        UserInfo u=userInfoService.selectu(user);
        //把数据传到前端
        m.addAttribute("user",u);
        return "user/user-info";
    }

    //处理修改的ajax请求
    @RequestMapping("/usinfo")
    @ResponseBody
    public HashMap<String,Object> usinfo(UserInfo user){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = userInfoService.userUpdate(user);
        System.out.print(info);
        map.put("info",info);
        return map;
    }

    //注销用户信息
    @RequestMapping("/deluser")
    @ResponseBody
    public HashMap<String,Object> deluser(UserInfo user){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = userInfoService.delUser(user);
        System.out.print(info);
        map.put("info",info);
        return map;
    }

    //访问当前用户的数据的页面服务请求页面
    @RequestMapping("/servicePage")
    public String servicePage( UserInfo user,UserSerInfo ser, ModelMap m, HttpSession session){
        //把当前用户名存入pojo中并备份中也存入
        UserInfo userInfo= (UserInfo) session.getAttribute("user");
        ser.setSerUserNickName(userInfo.getUserName());
        //查询分页数据
        HashMap<String, Object> map=userInfoService.selectuserser(user,ser);
        //把数据传到前端
        m.put("info",map);
        //把用户输入的查询条件传到前端
        m.put("vv",ser.getConValue());
        return "user/user-service";
    }



    //打开修改服务界面
    @RequestMapping("/serveeditPage")
    public String serveeditPage(UserSerInfo ser, ModelMap m){
        //根据userId查询
        UserSerInfo u =userInfoService.selectByserId(ser);
        //把数据传递到前端
        m.addAttribute("ser",u);
        return "user/user-addser";
    }
    //处理修改服务信息的ajax请求
    @RequestMapping("/serveedit")
    @ResponseBody
    public HashMap<String,Object> serveedit(UserSerInfo ser){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = userInfoService.upService(ser);
        map.put("info",info);
        return map;
    }

    //删除服务
    @RequestMapping("/delservice")
    @ResponseBody
    public HashMap<String,Object> delservice(UserSerInfo ser){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = userInfoService.delservice(ser);
        System.out.print(info);
        map.put("info",info);
        return map;
    }

    //打开新增服务界面
    @RequestMapping("/serveaddPage")
    public String serveaddPage(UserInfo user,UserSerInfo ser, ModelMap m, HttpSession session){
        //把当前用户名存入pojpo中并备份中也存入
        UserInfo userInfo= (UserInfo) session.getAttribute("user");
        ser.setSerUserNickName(userInfo.getUserName());
        //根据userId查询
        UserInfo u =userInfoService.selectUserSer(ser);
        //把数据传递到前端
        m.addAttribute("user",u);
        return "user/user-addservice";
    }
    //处理新增服务信息的ajax请求
    @RequestMapping("/serveadd")
    @ResponseBody
    public HashMap<String,Object> serveadd(UserSerInfo ser){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = userInfoService.serveadd(ser);
        map.put("info",info);
        return map;
    }

    //用户服务处理结果查询
    @RequestMapping("/resultPage")
    public String resultPage(Resultinfo resu, ModelMap m, HttpSession session){
        //把当前用户名存入pojpo中并备份中也存入
        UserInfo userInfo= (UserInfo) session.getAttribute("user");
        //把当前用户名存入pojpo中并备份中也存入
        resu.setResultName(userInfo.getUserName());
        //查询分页数据
        HashMap<String, Object> map=userInfoService.selectresult(resu);
        //把数据传到前端
        m.put("info",map);
        //把用户输入的查询条件传到前端
        m.put("vv",resu.getConValue());
        return "user/user-result";
    }
    //打开修改服务界面
    @RequestMapping("/resulteditPage")
    public String resulteditPage(Resultinfo result, ModelMap m){
        //根据userId查询
        Resultinfo u =userInfoService.selectByresultId(result);
        //把数据传递到前端
        m.addAttribute("ret",u);
        return "user/user-editresult";
    }
    //处理修改服务信息的ajax请求
    @RequestMapping("/resultedit")
    @ResponseBody
    public HashMap<String,Object> resultedit(Resultinfo result){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = userInfoService.uprequist(result);
        map.put("info",info);
        return map;
    }

    //访问修改密码界面updatePwd
    @RequestMapping("/updatePwd")
    public String updatePwd(){
        return "user/updatePwd";
    }
    //处理修改密码的ajax请求
    @RequestMapping("/updatePwdAjax")
    @ResponseBody
    public HashMap<String,Object> updatePwdAjax(UserInfo use, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = userInfoService.updatePwd(use,request);
        map.put("info",info);
        return map;
    }

    //访问修改头像页面
    @RequestMapping("/updateHead")
    public String updateHead(){
        return "user/updateHead";
    }

    //头像上传
    @RequestMapping("/upload")
    @ResponseBody
    public HashMap<String,Object> upload(MultipartFile file){
        HashMap<String,Object> map = new HashMap<String,Object>();

        //上传文件
        try {
            //定义一个文件上传路径
            String timeDir="";
            //获取时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            timeDir=sdf.format(new Date());

            //定义上传文件的前缀
            String pre="";
            //是为了保证文件上传后，存到服务器的文件名是唯一的
            pre= UUID.randomUUID()+"";

            //获取文件的后缀名
            String hou="";
            if(file!=null){
                //.jpg
                String originalName = file.getOriginalFilename();
                hou=originalName.substring(originalName.lastIndexOf(".")+1);
            }

            //定义文件上传的全路径
            String filePath=fileAddress+"\\"+timeDir+"\\"+pre+"."+hou;

            //创建file对象
            File f=new File(filePath);

            //判断目录是否存在，不存在就创建目录
            if (!f.isDirectory()){
                //创建目录
                f.mkdirs();
            }
            //上传文件
            file.transferTo(f);
            //设置成功提示信息
            map.put("code",0);
            //返回给前端 用户访问这个图片的路径
            String path=upload+"\\"+timeDir+"\\"+pre+"."+hou;
            map.put("src",path);
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置上传失败的提示信息
        map.put("code",1);
        return map;
    }

    //头像更改
    @RequestMapping("/saveHead")
    @ResponseBody
    public HashMap<String,Object> saveHead(UserInfo user, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();

        String info = userInfoService.updateHead(user,request);
        map.put("info",info);
        return map;
    }

}
